package com.shoppingmall.service.impl;

import com.google.common.collect.Sets;
import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.dao.CategoryMapper;
import com.shoppingmall.pojo.Category;
import com.shoppingmall.service.CategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by
 *
 * @author touni
 * @date 2019/3/19
 * @time 16:29
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;



    @Override
    public ServletResponse getCategoryByParentId(Integer parentId) {
        List<Category> resultList = categoryMapper.selectCategoryByParentId(parentId);
        if (null == resultList || resultList.size() <1){
            return ServletResponse.createByErrorMessage("未找到这个品类的子类");
        }


        return ServletResponse.createBySuccess(resultList);
    }

    @Override
    public ServletResponse addCategoryByParentId(Integer parentId, String categoryName) {
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);
        int flag = categoryMapper.insert(category);
        if (1 != flag){
            return ServletResponse.createByErrorMessage("新增品类失败");
        }


        return ServletResponse.createBySuccessMessage("新增品类成功");
    }

    @Override
    public ServletResponse updateCategoryNameByCategoryId(Integer categoryId, String categoryName) {
        //先查询是否有这个品类
        Category categoryFromQuery = categoryMapper.selectByPrimaryKey(categoryId);
        if (null == categoryFromQuery){
            return ServletResponse.createByErrorMessage("该品类不存在");

        }
        categoryFromQuery.setName(categoryName);
        int flag = categoryMapper.updateByPrimaryKey(categoryFromQuery);
        if (1 != flag){
            return ServletResponse.createByErrorMessage("修改失败");
        }
        return ServletResponse.createBySuccessMessage("更新品类名称成功");
    }

    /**
     * 需要获取这个节点和该节点下的所有子节点的ID
     * @param categoryId
     * @return
     */
    @Override
    public ServletResponse getTheSameParentIdByCategoryId(Integer categoryId) {
        //先查询这个品类
        Category categoryFromQuery = categoryMapper.selectByPrimaryKey(categoryId);
        if (null == categoryFromQuery){
            return ServletResponse.createByErrorMessage("该品类不存在");
        }
        HashSet<Category> categorySet = Sets.newHashSet();
        Set<Category> allCategoryById = getAllCategoryById(categorySet, categoryId);
        return ServletResponse.createBySuccess(allCategoryById);
    }

    /**
     * 迭代查询子节点和子节点下的子节点
     * @param categorySet
     * @param id
     * @return
     */
    public Set<Category> getAllCategoryById(Set<Category> categorySet,Integer id){
        //首先根据这个ID获取category 放入到set中
        Category category = categoryMapper.selectByPrimaryKey(id);
        categorySet.add(category);
        //判断这个category是否有子节点
        List<Category> categoryList = categoryMapper.selectCategoryByParentId(id);
        if (!CollectionUtils.isEmpty(categoryList)){
            //遍历子节点，迭代查询子节点是否还有子节点
            for (Category categories :categoryList){
                getAllCategoryById(categorySet,categories.getId());

            }
        }

        return categorySet;



    }




}
